//
//  HJBuyView.m
//  Sneakers
//
//  Created by 胡光健 on 2017/9/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HJBuyView.h"
#import "HJHomeModel.h"


@interface HJBuyView ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *shopImage;
@property (weak, nonatomic) IBOutlet UILabel *shapTitle;
@property (weak, nonatomic) IBOutlet UILabel *moneyLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *goodsNoAndTime;
@property (weak, nonatomic) IBOutlet UIView *line;
@property (weak, nonatomic) IBOutlet UIView *line1;



@end

@implementation HJBuyView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self = [[NSBundle mainBundle]loadNibNamed:@"HJBuyView" owner:nil options:nil].lastObject;
        self.frame = frame;
    }
    return self;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    self.priceTextField.delegate = self;
    self.priceTextField.inputAccessoryView = [self addToolbar];
    self.shapTitle.font = HJFontSize(mScreenWidth/414 *15);
    self.moneyLabel.font = HJFontSize(mScreenWidth/414 *19);
    self.goodsNoAndTime.font = HJFontSize(mScreenWidth/414 *13);
//    self.priceTextField.font = [UIFont fontWithName:@"Bold" size:mScreenWidth/414 *23];
    self.consultPrice.font = HJFontSize(mScreenWidth/414 *13);
    
}
- (UIToolbar *)addToolbar {
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, mScreenWidth, 35)];
    toolbar.tintColor = HJColorRGB(0);
    toolbar.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space, bar];
    return toolbar;
}
-(void)textFieldDone {
    if ([self.priceTextField.text isEqualToString:@"￥"]) {
        self.priceTextField.text = @"";
    }
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    textField.text =[NSString stringWithFormat:@"￥"];
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([textField.text isEqualToString:@"￥"]) {
        textField.text = @"";
    }
    return [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
-(void)textFieldDidEndEditing:(UITextField *)textField {
    [mNotificationCenter postNotificationName:@"priceTextFieldNotification" object:nil userInfo:@{@"text":textField.text}];
}

-(void)setShoesModel:(HJOderModel *)shoesModel {
    _shoesModel = shoesModel;
    [self.shopImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",shoesModel.shoes.avatar_base_url,shoesModel.shoes.avatar_path]] placeholderImage:nil];
    self.shapTitle.text = shoesModel.shoes.shoes_name;
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",shoesModel.shoes.sell_money];
    self.goodsNoAndTime.text = [NSString stringWithFormat:@"%@   %@",shoesModel.shoes.shoes_article_number,[CommonTools getTimeStrBytimeStamp:shoesModel.shoes.created_at]];
    self.priceTextField.text = [NSString stringWithFormat:@"￥%@",shoesModel.shoes.reference_price];
    self.consultPrice.text = [NSString stringWithFormat:@"参考价：%@",shoesModel.shoes.reference_price];
    [self.codeBtn setTitle:[NSString stringWithFormat:@"%@码",self.size] forState:UIControlStateNormal];
    
    if (!shoesModel.is_check) {
        [self animationLineWithLine:101 btn:[self viewWithTag:101]];
//        [self animationLineWithBtn:];
    }else {
//        [self animationLineWithBtn:];
        [self animationLineWithLine:self.currentTag btn:[self viewWithTag:self.currentTag]];
    }
   
}


// 购买100 求购101
- (IBAction)buyBtn:(UIButton *)sender {
    if (!_shoesModel.is_check) {
        [[UIViewController currentViewController] showErrorHUDWithError:@"无当前尺码出售。无法立刻购买"];
        return;
    }
    [self animationLineWithLine:sender.tag btn:sender];
  
    if (self.block) {
        self.block(sender.tag, sender);
    }
}
-(void)animationLineWithLine:(NSInteger)tag btn:(UIButton *)btn {
    if (tag==100) {
        [UIView animateWithDuration:0.1 animations:^{
            self.priceTextField.userInteractionEnabled = NO;
            self.priceTextField.text = [NSString stringWithFormat:@"￥%@",_shoesModel.shoes.reference_price];
            self.consultPrice.hidden = YES;
            self.line.hidden = NO;
            self.line1.hidden = YES;
        }];
    }else {
        [UIView animateWithDuration:0.1 animations:^{
            self.priceTextField.userInteractionEnabled = YES;
            self.priceTextField.text = @"";
            self.consultPrice.hidden = NO;
            self.line.hidden = YES;
            self.line1.hidden = NO;
        }];
    }
    [self animationLineWithBtn:btn];
}

// 滑动线条
-(void)animationLineWithBtn:(UIButton *)btn {
    for (int i = 100; i<=101; i ++) {
        UIButton *view = [self viewWithTag:i];
        view.alpha = 0.5;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            view.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:15];
        });
    }
    btn.alpha = 1;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
          btn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:15];
    });
}

- (IBAction)codeBtn:(UIButton *)sender {
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"codeNotification" object:nil];
}
@end
